
import {reqGoodsInfo,reqAddshopCart} from '../../api/index'
import { getUUID } from '@/utils/uuid_token'

const state={ 
   goodsInfo:{},
   addshop:{},
   //游客临时身份信息
   uuid_token:getUUID(),
}
const actions={
   //通过API里面的接口调用，向服务器发送请求，获取服务器的数据
   getGoodsInfo(context,skuId){
      let result =reqGoodsInfo(skuId)
      result.then(Request=>{
         if(Request.code===200){
         context.commit('GETGOODSINFO',Request.data)
         }
      },error=>{
         console.log('请求发送失败',error.message)
      })
   },
  //将产品添加到购物车中
  //发送请求以后，将参数带给服务器，服务器写入数据成功，但并没有返回新的数据
  //只有code==200，代表成功
  addshopCart(context,{skuId,skuNum}){
   let result =reqAddshopCart(skuId,skuNum)
   result.then(Request=>{
      if(Request.code===200){
       return 'ok'
      }else{
        return Promise.reject("错误！")
      }
   },error=>{
      console.log('请求发送失败',error.message)
   })
},
}

 
const mutations={
   GETGOODSINFO(state,value){
  state.goodsInfo=value
   },
}
const getters={
   categoryView(state){
      return state.goodsInfo.categoryView||{}
   },
   skuInfo(state){
      return state.goodsInfo.skuInfo||{}
   },
   spuSaleAttrList(state){
      return state.goodsInfo.spuSaleAttrList||[]
   },
   }
export default{
   state,actions,mutations,getters
}
